﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell;
using System.IO;

namespace Liekhus.Xaf.BusinessObjectDesigner.CustomCode
{
    public class XAFBODRegistrationAttribute : RegistrationAttribute
    {
        private string _packageGuid;
        private string _generatorClsid;
        private string _editorFactoryGuid;
        private Type _generatorType;

        private const string CSharpGeneratorsGuid = "{fae04ec1-301f-11d3-bf4b-00c04f79efbc}";
        private const string CSharpProjectGuid = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}";

        public XAFBODRegistrationAttribute(string packageGuid,
               string generatorClsid,
               string editorFactoryGuid, Type generatorType)
        {
            if (packageGuid == null)
                throw new ArgumentNullException("packageGuid");
            if (generatorClsid == null)
                throw new ArgumentNullException("generatorClsid");
            if (editorFactoryGuid == null)
                throw new ArgumentNullException("editorFactoryGuid");
            if (generatorType == null)
                throw new ArgumentNullException("generatorType");

            _packageGuid = packageGuid;
            _generatorClsid = generatorClsid;
            _generatorType = generatorType;
            _editorFactoryGuid = editorFactoryGuid;
        }

        public override void Register(RegistrationAttribute.RegistrationContext context)
        {
            try
            {
                context.Log.Write("Registering Liekhus XAF/DSL Generator ... ");

                // First we are registering our class

                Key key = context.CreateKey(@"CLSID");
                Key subKey = key.CreateSubkey(_generatorClsid);
                subKey.SetValue("ThreadingModel", "Both");
                subKey.SetValue("InprocServer32", Path.Combine(Environment.SystemDirectory, "mscoree.dll"));
                subKey.SetValue("Class", _generatorType.FullName);
                subKey.SetValue("Assembly", _generatorType.Assembly.FullName);
                subKey.Close();
                key.Close();

                // Then we are registering our custom generator

                key = context.CreateKey(@"Generators\" + CSharpGeneratorsGuid);
                subKey = key.CreateSubkey("LiekhusXafBusinessObjectDesigner");
                subKey.SetValue(string.Empty, "Liekhus XAF/DSL Designer");
                subKey.SetValue("CLSID", _generatorClsid);
                subKey.SetValue("GeneratesDesignTimeSource", 1);
                subKey.Close();
                key.Close();

                // register .sl editor notification


                key = context.CreateKey(@"Projects\" + CSharpProjectGuid + @"\FileExtensions");
                subKey = key.CreateSubkey(".liekhusxafbodesigner");
                subKey.SetValue("EditorFactoryNotify", _editorFactoryGuid);
                subKey.Close();
                key.Close();
                context.Log.WriteLine("Success.");
            }
            catch (Exception e)
            {
                context.Log.WriteLine("Failure: " + e);
            }
        }

        public override void Unregister(
               RegistrationAttribute.RegistrationContext context)
        {
            try
            {
                context.Log.Write("Unregistering Example generator... ");
                context.RemoveKey(@"CLSID\" + _generatorClsid);
                context.RemoveKey(@"Generators\" + CSharpGeneratorsGuid + @"\LiekhusXafBusinessObjectDesigner");
                context.RemoveKey(@"Projects\" +
                   CSharpProjectGuid + @"\FileExtensions\.liekhusxafbodesigner");
                context.Log.WriteLine("Success.");
            }
            catch (Exception e)
            {
                context.Log.WriteLine("Failure: " + e);
            }
        }
    }
}
